package org.iamd.security;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.iamd.conf.ConfigurationFactory;
import org.iamd.conf.InitializationException;

/**
 * Инициализиране на списъка с услугите, които мениджъра на сигурността
 * предоставя.
 * 
 * @author И. Атанасов
 */
public class SecurityConfigurationFactory implements ConfigurationFactory {

	/**
	 * @see org.iamd.conf.ConfigurationFactory#initializeComponent(java.util.Map,
	 *      java.io.File)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void initializeComponent(Map<String, String> params, File configFile)
			throws InitializationException {
		try {
			Map<Class<? extends ISecurityService>, ISecurityService> servicesMap = new HashMap<Class<? extends ISecurityService>, ISecurityService>();
			SecurityServicesManager instance = SecurityServicesManager.getInstance();
			for (String key : params.keySet()) {
				Class<? extends ISecurityService> interfClass = (Class<? extends ISecurityService>) Class
						.forName(key);
				Class<?> implClazz = Class.forName(params.get(key));
				Constructor<?> constr = implClazz.getDeclaredConstructor(new Class[] {});
				constr.setAccessible(true);
				servicesMap.put(interfClass, interfClass.cast(constr.newInstance(new Object[] {})));
			}
			instance.init(servicesMap);
		} catch (Exception e) {
			throw new InitializationException(e);
		}
	}

}